<?php
include_once('Sqlexecutor.php');
class CorpInfo
{
    private $_dbh;
    private $_logger;

    public function __construct($dbh, $logger = Null)
    {/*{{{*/
        $this->_dbh = $dbh; 
        $this->_logger = $logger; 
    }/*}}}*/

    public function getInfo()
    {/*{{{*/
        $sql = "select `txt1`, `txt2`, `txt3`, `txt4`, `txt5`, `txt6`, `txt7` from bs_corp_info where `id` = 1";
        try
        {
            $info = $this->_dbh->query($sql);
            if($info === false)
                $info = NULL;
            return $info;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return false;
        }
    }/*}}}*/

    public function create($txt1, $txt2, $txt3, $txt4, $txt5, $txt6, $txt7)
    {/*{{{*/
        $sql = "insert into bs_corp_info set `id` = 1, `txt1` = ?, `txt2` = ?, `txt3` = ?, `txt4` = ?, `txt5` = ?, `txt6` = ?, `txt7` = ?";
        $val[] = $txt1;
        $val[] = $txt2;
        $val[] = $txt3;
        $val[] = $txt4;
        $val[] = $txt5;
        $val[] = $txt6;
        $val[] = $txt7;
        try
        {
            $this->_dbh->beginTrans();
            $this->_dbh->execute($sql, $val); 
            $lastId = $this->_dbh->getLastInsertID();
            $this->_dbh->commit();

            return $lastId;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

    public function update($txt1, $txt2, $txt3, $txt4, $txt5, $txt6, $txt7)
    {/*{{{*/
        $sql = 'update bs_corp_info set `txt1` = ?, `txt2` = ?, `txt3` = ?, `txt4` = ?, `txt5` = ?, `txt6` = ?, `txt7` = ? where `id` = 1';
        $val[] = $txt1;
        $val[] = $txt2;
        $val[] = $txt3;
        $val[] = $txt4;
        $val[] = $txt5;
        $val[] = $txt6;
        $val[] = $txt7;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

    public function delete()
    {
        $sql = "delete from bs_corp_info where `id` = 1";
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql); 
            $this->_dbh->commit();

            // 0: not delete, 1 : delete 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            
            // Unknow error
            return -100;
        }

    }

}

/* example 
include('log4php/Logger.php');
$dbh = new Sqlexecutor('localhost', 'tile', 'tile', 'tile');
$logger = Logger::getLogger('intf');
$s = new CorpInfo($dbh, $logger);
$res = $s->create("1", "2", "3", "4", "5", "6", "7");
var_dump($res);
$res = $s->getInfo();
var_dump($res);
$res = $s->create("1", "2", "3", "4", "5", "6", "7");
var_dump($res);
$res = $s->update("a", "b", "c", "d", "e", "f", "g");
var_dump($res);
$res = $s->getInfo();
var_dump($res);
$res = $s->delete();
var_dump($res);
*/
